#include "StdAfx.h"
#include "Log.h"

#define LOG_FILENAME "./Fossil.log"

using namespace Fossil;
using namespace std;
void Log::Initialise(LogLevel Level/* = LogSeverityWarning*/)
{
#if defined(LOG_ENABLE)
	m_LogLevel = Level;
	//Open the log file, since we use tasty HTML logging, we need to set some things up

	/*
	<style type="text/css">
	hr {color:sienna}
	p {margin-left:20px}
	body {background-image:url("images/back40.gif")}
	</style>
	*/

	m_FileStream.open (LOG_FILENAME, ios::out | ios::trunc);
	m_FileStream << "<HTML>" << endl;
	m_FileStream << "\t<head>" << endl;

	//Inline CSS, *shudder*
	m_FileStream << "\t\t<style type=\"text/css\">" << endl;

	//Body
	m_FileStream << "\t\t\tbody {" << endl;
	m_FileStream << "\t\t\t\tbackground-color:FFFFFF;" << endl;
	m_FileStream << "\t\t\t\tfont-family:Arial;" << endl;
	m_FileStream << "\t\t\t\tfont-size:12px;" << endl;
	m_FileStream << "\t\t\t}" << endl;

	//Common 
	m_FileStream << "\t\t\t.log {" << endl;
	m_FileStream << "\t\t\t\twidth:100%;" << endl;
	m_FileStream << "\t\t\t\tline-height:20px;" << endl;
	m_FileStream << "\t\t\t\tpadding:2px 10px;" << endl;
	m_FileStream << "\t\t\t}" << endl;

	//Trivial class
	m_FileStream << "\t\t\t.trivial {" << endl;
	m_FileStream << "\t\t\t\tbackground-color:77FD68;" << endl;
	m_FileStream << "\t\t\t}" << endl;

	//Warning class
	m_FileStream << "\t\t\t.warning {" << endl;
	m_FileStream << "\t\t\t\tbackground-color:FFFF77;" << endl;
	m_FileStream << "\t\t\t}" << endl;

	//Error class
	m_FileStream << "\t\t\t.error {" << endl;
	m_FileStream << "\t\t\t\tbackground-color:FF6666;" << endl;
	m_FileStream << "\t\t\t}" << endl;

	//Unknown class
	m_FileStream << "\t\t\t.unknown {" << endl;
	m_FileStream << "\t\t\t\tbackground-color:8D67FE;" << endl;
	m_FileStream << "\t\t\t}" << endl;

	m_FileStream << "\t\t</style>" << endl;

	m_FileStream << "\t</head>" << endl;
	m_FileStream << "\t<body>" << endl;
	m_FileStream.close();
#endif//defined(LOG_ENABLE)
}

void Log::Destroy()
{
#if defined(LOG_ENABLE)
	//Try to make it nice and tidy html
	m_FileStream.open (LOG_FILENAME, ios::out | ios::app);
	m_FileStream << "\t<body>" << endl;
	m_FileStream << "</HTML>" << endl;
	m_FileStream.close();
#endif//defined(LOG_ENABLE)
}

void Log::LogMessage(LogLevel Level, const char* pMessage)
{
#if defined(LOG_ENABLE)
	if((!m_FileStream.is_open()) && (Level >= m_LogLevel))//Checking if the stream is open may be enough to make it thread-safe
	{
		m_FileStream.open (LOG_FILENAME, ios::out | ios::app);
		m_FileStream << "\t\t<div class=\"log " << GetLogLevelClass(Level) << "\"> " << pMessage << " </div>" << endl;
		m_FileStream.close();
	}
#endif//defined(LOG_ENABLE)
}

const char* Log::GetLogLevelClass(LogLevel Level)
{
	switch(Level)
	{
	case LogLevelTrivial:
		return "trivial";
	break;
	case LogLevelWarning:
		return "warning";
	break;
	case LogLevelError:
		return "error";
	break;
	}

	return "unknown";
}
